<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\common\controller\Backend;
use fast\Redis;
use think\Config;
use think\Hook;
use think\Validate;
use Jasny\SSO\NotAttachedException;
use Jasny\SSO\Exception as SsoException;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        //左侧菜单
        list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
            'dashboard' => 'hot',
            'addon' => ['new', 'red', 'badge'],
            'auth/rule' => __('Menu'),
            'general' => ['new', 'purple'],
        ], $this->view->site['fixedpage']);

        $action = $this->request->request('action');
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
            }
        }
        $this->view->assign('menulist', $menulist);
        $this->view->assign('navlist', $navlist);
        $this->view->assign('fixedmenu', $fixedmenu);
        $this->view->assign('referermenu', $referermenu);
        $this->view->assign('title', __('Home'));

        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $this->redirect(config('mp.url') . '/admin/index/login?redirect_url=' . urlencode(config('app_url')));
        return;
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        if (config('sso.open')) {
            $this->broker->logout();
        }
        Hook::listen("admin_logout_after", $this->request);
        $this->success(__('Logout successful'), 'index/login');
    }

}
